package com.zm.zb100.service;

import com.zm.zb100.persistence.entity.db.EpsCommodityAttractDesign;
import com.zm.zb100.repository.EpsCommodityAttractDesignRepository;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author chuanbai
 * @date 2024/3/29 1:05
 * @deception
 */
@Service
@Slf4j
public class EpsCommodityAttractDesignService{

    @Resource
    private EpsCommodityAttractDesignRepository epsCommodityAttractDesignRepository;

    @Transactional
    public Boolean insertEpsCommodityAttractDesign(EpsCommodityAttractDesign epsCommodityAttractDesign){
        try {
            EpsCommodityAttractDesign save = epsCommodityAttractDesignRepository.save(epsCommodityAttractDesign);
            return Boolean.TRUE;
        }catch (Exception e){
            // log.error("添加引流款失败，引流款数据：{}",epsCommodityAttractDesign);
            return Boolean.FALSE;
        }
    }

    @Transactional
    public List<EpsCommodityAttractDesign> getByChannel(String channel){
        try {
            List<EpsCommodityAttractDesign> objs = epsCommodityAttractDesignRepository.findEpsCommodityAttractDesignByChannel(channel);
            return objs;
        }catch (Exception e){
            // log.error("添加引流款失败，引流款数据：{}",epsCommodityAttractDesign);
            return null;
        }
    }

    public Boolean insertList(List<EpsCommodityAttractDesign> epsCommodityAttractDesignList){
        List<EpsCommodityAttractDesign> designs = epsCommodityAttractDesignRepository.saveAll(epsCommodityAttractDesignList);
        return Boolean.TRUE;
    }

    @Transactional
    public Boolean updateList(List<EpsCommodityAttractDesign> epsCommodityAttractDesigns){
        epsCommodityAttractDesigns.forEach(dto -> {
            epsCommodityAttractDesignRepository.updateEpsCommodityAttractDesign(Long.valueOf(111111),
                    "klys",new Date(),"ABC123","郑州", "Channel ABC"
            );
        });
        return Boolean.TRUE;
    }

    @Transactional
    public Boolean deleteList(List<EpsCommodityAttractDesign> epsCommodityAttractDesignList){
        epsCommodityAttractDesignList.stream()
                .forEach(dto->{
                    epsCommodityAttractDesignRepository.deleteList(dto.getDesignNo(),dto.getEpsChannelName());
                });
        return Boolean.TRUE;
    }

    @Transactional
    public Boolean deleteByOne(List<EpsCommodityAttractDesign> epsCommodityAttractDesignList){
        epsCommodityAttractDesignList.stream()
                .forEach(dto->{
                    epsCommodityAttractDesignRepository.delete(dto);
                });
        return Boolean.TRUE;
    }

    public Page findPage(){
        PageRequest pageRequest = PageRequest.of(0, 2);
        Page<EpsCommodityAttractDesign> page = epsCommodityAttractDesignRepository.findAll(pageRequest);
        return page;
    }
}
